package com.huarui.receiptAddress.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.xwork.StringUtils;
import org.ewaf.framework.action.BaseAction;
import org.ewaf.framework.model.ListModel;
import org.ewaf.framework.util.HttpUtil;

import com.huarui.area.model.Area;
import com.huarui.area.service.AreaService;
import com.huarui.receiptAddress.model.ReceiptAddress;
import com.huarui.receiptAddress.service.ReceiptAddressService;
import com.huarui.user.model.SessionMessage;
import com.huarui.user.security.UserHolder;

public class ReceiptAddressAction extends BaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String dir;
	private String sort;
	private int start;
	private int limit;
	private SessionMessage sessionMessage;
	private ReceiptAddressService receiptAddressService;
	private List<ReceiptAddress> receiptAddressList;
	private AreaService areaService;
	Map<String, Object> resultMap = new HashMap<String, Object>();

	public Map<String, Object> getResultMap() {
		return resultMap;
	}

	public void setAreaService(AreaService areaService) {
		this.areaService = areaService;
	}

	public SessionMessage getSessionMessage() {
		return sessionMessage;
	}

	public void setSessionMessage(SessionMessage sessionMessage) {
		this.sessionMessage = sessionMessage;
	}

	public ReceiptAddressService getReceiptAddressService() {
		return receiptAddressService;
	}

	public void setReceiptAddressService(
			ReceiptAddressService receiptAddressService) {
		this.receiptAddressService = receiptAddressService;
	}

	public List<ReceiptAddress> getReceiptAddressList() {
		return receiptAddressList;
	}

	public void setReceiptAddressList(List<ReceiptAddress> receiptAddressList) {
		this.receiptAddressList = receiptAddressList;
	}

	public String getDir() {
		return dir;
	}

	public void setDir(String dir) {
		this.dir = dir;
	}

	public String getSort() {
		return sort;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public String index() {

		sessionMessage = UserHolder.getCurrentSessionMessage();

		return "index";
	}

	@SuppressWarnings("unchecked")
	public String query() throws IOException {

		sessionMessage = UserHolder.getCurrentSessionMessage();

		Integer companyId =sessionMessage.getCompanyId();
				
		ListModel<ReceiptAddress> lm = new ListModel<ReceiptAddress>();
		lm.setPageSize(limit);
		lm.setStart(start);
		lm.setOrderBy(sort);
		lm.setDesc("desc".equals(dir) ? true : false);
           
		ListModel<ReceiptAddress> listModel = null;

		try {
			listModel = receiptAddressService.queryReceiptAddressByCompanyId(
					companyId, lm);
		} catch (Exception e) {

			resultMap.put("failture", "服务器正则升级中...");

			return "json";
		}
		JSONArray root = JSONArray.fromObject(listModel.getResult());

		// 分页信息
		resultMap.put("totalProperty", listModel.getTotal());
		resultMap.put("page", lm.getPage());
		resultMap.put("root", root);

		return "json";

	}

	
	
	
	@SuppressWarnings("unchecked")
	public String save() throws IOException {

		sessionMessage = UserHolder.getCurrentSessionMessage();

		Integer companyId = sessionMessage.getCompanyId();

		HttpServletRequest request = HttpUtil.getRequest();

		String jsonAll = request.getParameter("jsonAll");

		JSONObject jsonObject = JSONObject.fromObject(jsonAll);

		ReceiptAddress receiptAddress = (ReceiptAddress) JSONObject.toBean(
				jsonObject, ReceiptAddress.class);
		
		
		Area area = (Area) areaService.getById(receiptAddress.getAreaId());

		if (area != null) {
			receiptAddress.setProvince(area.getProvince());
			receiptAddress.setCity(area.getCity());
			receiptAddress.setDistrict(area.getDistrict());
		}
		if (receiptAddress.getId() != null) {
					
	        Map map = new HashMap();
			
			map.put("areaId", receiptAddress.getAreaId());
			map.put("street",receiptAddress.getStreet());
			map.put("postCode", receiptAddress.getPostCode());
			map.put("receiverName",receiptAddress.getReceiverName());
			map.put("phone", receiptAddress.getPhone());
			map.put("mobile", receiptAddress.getMobile());
			map.put("companyId",companyId);
			
	        ReceiptAddress  ra = receiptAddressService.getReceiptAddressByAreaId(map);
			
	         if(ra!=null){
	        	 
	        	 if(!receiptAddress.getId().equals(ra.getId())){
	        		resultMap.put("failure", "收获地址名称不能重复");

					return "json";
	        	 }
	         }
			
			try {
				
				ReceiptAddress  rs = (ReceiptAddress)receiptAddressService.getById(receiptAddress.getId());
				rs.setCompanyId(companyId);
				rs.setAreaId(receiptAddress.getAreaId());
				rs.setCity(receiptAddress.getCity());
				rs.setPhone(receiptAddress.getPhone());
				rs.setDistrict(receiptAddress.getDistrict());
				rs.setMobile(receiptAddress.getMobile());
				rs.setPostCode(receiptAddress.getPostCode());
				rs.setProvince(receiptAddress.getCity());
				rs.setDistrict(receiptAddress.getDistrict());
				rs.setReceiverName(receiptAddress.getReceiverName());

				receiptAddressService.update(receiptAddress);
			} catch (Exception e) {

				resultMap.put("failture", "更新失败");

				return "json";
			}
		} else {
			
	        Map map = new HashMap();
			
			map.put("areaId", receiptAddress.getAreaId());
			map.put("street",receiptAddress.getStreet());
			map.put("postCode", receiptAddress.getPostCode());
			map.put("receiverName",receiptAddress.getReceiverName());
			map.put("phone", receiptAddress.getPhone());
			map.put("mobile", receiptAddress.getMobile());
			map.put("companyId",companyId);
			
	        ReceiptAddress  ra = receiptAddressService.getReceiptAddressByAreaId(map);
			
	         if(ra!=null){
	        	 
	        		resultMap.put("failure", "收获地址名称不能重复");

					return "json";
	         }
			
			try {

				receiptAddress.setCompanyId(companyId);

				receiptAddressService.create(receiptAddress);

			} catch (Exception e) {

				resultMap.put("failture", "保存失败");

				return "json";

			}
		}

		resultMap.put("success", "OK");
		resultMap.put("receiptAddress", receiptAddress);

		return "json";
	}

	public String delete() throws IOException {

		HttpServletRequest request = HttpUtil.getRequest();

		String id = request.getParameter("id");

		if (id != null) {

			Integer receiptAddressId = Integer.valueOf(id);

			try {

				receiptAddressService.deleteById(receiptAddressId);

			} catch (Exception e) {

				resultMap.put("failture", "删除失败");

				return "json";

			}
		} else {

			resultMap.put("failture", "该对象不存在");

			return "json";
		}

		return null;

	}

}
